package com.ebt.m.policy;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import com.ebt.m.R;
import com.ebt.m.commons.utils.ActivityUtils;
import com.ebt.m.policy.bean.EventFinishAct;
import com.ebt.m.policy.bean.EventSearchTagClicked;
import com.ebt.m.policy.view.SearchHistoryListview;
import com.ebt.m.policy.view.SearchListview;
import com.ebt.m.proposal_v2.bean.temp.ActivityFinishStack;
import com.ebt.m.utils.DataValidation;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.view.ClearEditText;
import com.ebt.m.widget.BaseActivity;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 在线保单-搜索
 */
public class ActSearch extends BaseActivity {
    public static final String ARG_PARAM = "Param";

    @BindView(R.id.btn_cancel)
    FrameLayout btnCancel;
    @BindView(R.id.btn_search)
    FrameLayout btnSearch;
    @BindView(R.id.search_view)
    ClearEditText searchView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.progress_bar)
    ProgressBar mProgressBar;

    private SearchHistoryListview specProductSearchHistories;
    private SearchListview specProductSearchListView;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_online_policy_search);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        ButterKnife.bind(this);
        initViews();
    }

    @Override
    protected void init() {
        super.init();
        initHistories();
        ActivityFinishStack.getInstance().add(this);
    }


    private void initHistories() {
        content.removeAllViews();
        specProductSearchHistories = new SearchHistoryListview(this);
        content.addView(specProductSearchHistories);
        specProductSearchHistories.update(5);
    }

    private void search() {
        String keyword = searchView.getText().toString().trim();
        if (DataValidation.isEmpty(keyword)) {
            UIHelper.makeToast(this, "请输入搜索关键字！");
            return;
        }
        specProductSearchListView = new SearchListview(this);
        specProductSearchListView.update(keyword);
        content.removeAllViews();
        content.addView(specProductSearchListView);
        ActivityUtils.closeInput(this);
    }


    @OnClick({R.id.btn_cancel, R.id.btn_search, R.id.search_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.btn_search:
                search();
                break;
        }
    }

    /**
     */
    @Subscribe
    public void onEvent(EventSearchTagClicked eventSearchTagClicked) {
        if (eventSearchTagClicked != null && eventSearchTagClicked.getSearchHistory() != null) {
            searchView.setText(eventSearchTagClicked.getSearchHistory().getKeyword());
            searchView.setSelection(searchView.getText().toString().length());
            btnSearch.performClick();
        }
    }

    @Subscribe
    public void onEvent(EventFinishAct event) {
        finish();
    }
}
